#-*- coding: utf-8 -*-

import action as act

#缩放
def Scale(obj,dAction):
	duration=dAction["时长"]
	scale	=dAction["比例"]
	oAct	=act.ScaleTo(scale,duration)
	obj.do(oAct)

#拉伸
def ScaleXY(obj,dAction):
	duration=dAction["时长"]
	scalex 	=dAction["X"]
	scaley 	=dAction["Y"]
	oAct 	=act.ScaleXY(scalex,scaley,duration)
	obj.do(oAct)

#移动
def MoveBy(obj,dAction):
	duration=dAction["时长"]
	dx 	=dAction["dX"]
	dy 	=dAction["dY"]
	oAct 	=act.MoveBy((dx,dy),duration)
	obj.do(oAct)

#移至
def MoveTo(obj,dAction):
	duration=dAction["时长"]
	posx 	=dAction["X"]
	posy 	=dAction["Y"]
	oAct 	=act.MoveTo((posx,posy),duration)
	obj.do(oAct)

#旋转,角度大于0为顺时针方向
def Rotate(obj,dAction):
	duration=dAction["时长"]
	angle	=dAction["角度"]
	oAct 	=act.Rotate(angle,duration)
	obj.do(oAct)

#转至,角度大于0为顺时针方向,若angle大于360转angle%360
def RotateTo(obj,dAction):
	duration=dAction["时长"]
	angle	=dAction["角度"]
	oAct	=act.RotateTo(angle,duration)
	obj.do(oAct)

#跳跃
def Jump(obj,dAction):
	duration=dAction["时长"]
	posx	=dAction["X"]
	posy	=dAction["Y"]
	height	=dAction["高度"]
	times	=dAction["次数"]
	oAct 	=act.JumpBy( (posx,posy), height, times, duration)
	obj.do(oAct)

#淡入
def FadeIn(obj,dAction):
	duration=dAction["时长"]
	oAct    =act.FadeIn(duration)
	obj.do(oAct)

#淡至
def FadeTo(obj,dAction):
	duration=dAction["时长"]
	alpha	=dAction["透明度"]
	OnAc    =act.FadeTo(alpha,duration)
	obj.do(oAct)

#淡出
def FadeOut(obj,dAction):
	duration=dAction["时长"]
	oAct    =act.FadeOut(duration)
	obj.do(oAct)

#闪烁
def Blink(obj,dAction):
	duration=dAction["时长"]
	times	=dAction["次数"]
	oAct    =act.Blink(times,duration)
	obj.do(oAct)

#延迟
def Delay(obj,dAction):
	duration=dAction["时长"]
	oAct    =act.Delay(duration)
	obj.do(oAct)

#随机延迟,随机延迟low至hi秒
def Delay(obj,dAction):
	low =dAction["最小时长"]
	hi  =dAction["最大时长"]
	oAct=act.RandomDelay(low,hi)
	obj.do(oAct)

#回调
def Callback(obj,dAction):
	if not hasattr(obj,"m_Callback"):
		return
	func=obj.m_Callback
	del obj.m_Callback
	func()

def OnAction(obj,dStep):
	tAction=dStep["行为"]
	for iAct in tAction:
		if not iAct in obj.m_Action:
			raise Exception("Ani(%d) Action(%d) Not Exists"%(obj.m_SID,iAct))
		dAction=obj.m_Action[iAct]
		sAct=dAction["行为"]
		if not sAct in Actions:
			raise Exception("Not Implemented Action(%s)"%sAct)
		func=Actions[sAct]
		func(obj,dAction)


Actions={
	"缩放":Scale,
	"拉伸":ScaleXY,
	"移动":MoveBy,
	"移至":MoveTo,
	"旋转":Rotate,
	"转至":RotateTo,
	"跳跃":Jump,
	"淡入":FadeIn,
	"淡至":FadeTo,
	"淡出":FadeOut,
	"闪烁":Blink,
	"延迟":Delay,
	"随机延迟":RandomDelay,
	"回调":Callback,
}